package cn.magic0610.realtime;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Functions {

    public static YamlConfiguration config = null;
    public static BukkitRunnable task = null;
    public static List<World> changedWorlds = new ArrayList<>();
    public WeatherEvent event = new WeatherEvent();

    public void createConfig() {
        File configFile = new File(Realtime.plugin.getDataFolder(), "config.yml");
        try {
            if (Realtime.plugin.getDataFolder().mkdir() | configFile.createNewFile()) {
                Realtime.plugin.getLogger().info("Creating config.yml!");
            }
            FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
            config.addDefault("sync-time.sec", 30);
            config.addDefault("sync-time.disable-worlds", new ArrayList<>(Arrays.asList("exampleWorld-1", "exampleWorld-2")));
            config.addDefault("no-weather-change.enable", false);
            config.addDefault("no-weather-change.disable-worlds", new ArrayList<>(Arrays.asList("exampleWorld-1", "exampleWorld-2")));
            config.options().copyDefaults(true);
            config.save(configFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void load() {
        File configFile = new File(Realtime.plugin.getDataFolder(), "config.yml");
        config = YamlConfiguration.loadConfiguration(configFile);

        if (task != null) {
            task.cancel();
        }

        changeDayNight(changedWorlds, true);
        changedWorlds = getChangableWorlds(config.getStringList("sync-time.disable-worlds"));
        changeDayNight(changedWorlds, false);

        task = new TimeSyncer();
        task.runTaskTimer(Realtime.plugin, 0L, config.getLong("sync-time.sec") * 20);

        if (config.getBoolean("no-weather-change.enable")) {
            Bukkit.getPluginManager().registerEvents(event, Realtime.plugin);
        } else {
            HandlerList.unregisterAll(event);
        }
    }

    public void changeDayNight(List<World> worlds, Boolean value) {
        for (World world : worlds) {
            world.setGameRuleValue("doDaylightCycle", value.toString());
        }
    }

    public List<World> getChangableWorlds(List<String> worlds) {
        List<World> changableWorlds = new ArrayList<>();
        for (World world : Bukkit.getWorlds()) {
            if (worlds.contains(world.getName())) {
                continue;
            }
            if (Boolean.parseBoolean(world.getGameRuleValue("doDaylightCycle"))) {
                changableWorlds.add(world);
            }
        }
        return changableWorlds;
    }
}
